package cn.openea.perpartner.meily.web.controller;

import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.PersonalInfo;
import cn.openea.perpartner.meily.web.entity.Provider;
import cn.openea.perpartner.meily.web.entity.dto.PersonalInfoDTOFactory;
import cn.openea.perpartner.meily.web.entity.dto.ProviderDTOFactory;
import cn.openea.perpartner.meily.web.entity.dto.SearchDto;
import cn.openea.perpartner.meily.web.entity.vo.PersonalInfoSearchVo;
import cn.openea.perpartner.meily.web.entity.vo.PersonalInfoVo;
import cn.openea.perpartner.meily.web.entity.vo.ProviderVo;
import cn.openea.perpartner.meily.web.service.IPersonalInfoService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (PersonalInfo)表控制层
 *
 * @author rains
 * @since 2023-03-07 15:44
 */
@Api(tags = "个人资料")
@RestController
@RequestMapping("/personalInfo")
public class PersonalInfoController extends BaseController {

    @Resource
    private IPersonalInfoService personalInfoService;
    @Resource
    private PersonalInfoDTOFactory personalInfoDTOFactory;

    @ApiOperation("新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PersonalInfo personalInfo) {
        return toAjax(this.personalInfoService.save(personalInfo));
    }

    @ApiOperation("更新个人资料")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody PersonalInfo personalInfo) {
        return this.personalInfoService.update(personalInfo);
    }

    @ApiOperation("删除测评师")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody List<Long> idList) {
        return toAjax(this.personalInfoService.removeByIds(idList));
    }

    @ApiOperation("导出测评师列表")
    @PostMapping("/list/export")
    public void exportList(@RequestBody PersonalInfoVo vo, HttpServletResponse response) {
        List<PersonalInfo> personalInfos;
        if (vo.byIds()) {
            // 根据id去查
            personalInfos = personalInfoService.findByIds(vo.getIds());
        } else {
            personalInfos = personalInfoService.getList(vo.getSearchVo());
        }

        List<PersonalInfoDTOFactory.ExportDTO> exportDTOList = personalInfos
                .stream()
                .map(item -> personalInfoDTOFactory.toExportDTO.apply(item))
                .collect(Collectors.toList());
        // 导出
        ExcelUtil<PersonalInfoDTOFactory.ExportDTO> util = new ExcelUtil<>(PersonalInfoDTOFactory.ExportDTO.class);
        util.exportExcel(response, exportDTOList, "测评师.xlsx");
    }

    @ApiOperation("获取个人资料")
    @PostMapping("/getPersonalInfo")
    public AjaxResult getPersonalInfo(@RequestBody JSONObject object) {
        if (object.isEmpty()) {
            return AjaxResult.error("获取不到参数");
        }
        return this.personalInfoService.getPersonalInfo(object);
    }

    @ApiOperation("获取个人资料列表---分页")
    @PostMapping("/getList")
    public TableDataInfo getPersonalInfoList(@RequestBody SearchDto searchDto) {
        startPage();
        List<PersonalInfo> list = this.personalInfoService.getPersonalInfoList(searchDto);
        return getDataTable(list);
    }

    @ApiOperation("【测评】获取热门测评师")
    @PostMapping("/getPopularEvaluators")
    public AjaxResult getPopularEvaluators() {
        return this.personalInfoService.getPopularEvaluators();
    }

    @ApiOperation("【测评】获取测评师---换一批")
    @PostMapping("/changeEvaluators")
    public AjaxResult changeEvaluators(@RequestBody JSONObject object) {
        return this.personalInfoService.changeEvaluators(object);
    }

    @ApiOperation("【测评】获取测评师列表---分页")
    @PostMapping("/getEvaluatorsList")
    public TableDataInfo getEvaluatorsList(@RequestBody JSONObject object) {
        startPage();
        List<PersonalInfo> list = this.personalInfoService.getEvaluatorsList(object);
        return getDataTable(list);
    }

    @ApiOperation("【测评】校验测评师资料是否填写完毕")
    @PostMapping("/CheckEvaluatorInfo")
    public AjaxResult CheckEvaluatorInfo(@RequestBody JSONObject object) {
        return this.personalInfoService.CheckEvaluatorInfo(object);
    }

    @ApiOperation("【线上服务商】校验资料线上服务商是否填写完毕")
    @PostMapping("/checkProviderInfo")
    public AjaxResult checkProviderInfo(@RequestBody JSONObject object) {
        return this.personalInfoService.checkProviderInfo(object);
    }

    @ApiOperation("根据用户id修改测评师等级")
    @PostMapping("/updateEvaluationGradeByUserId")
    public AjaxResult updateEvaluationGradeByUserId(@RequestBody JSONObject object) {
        return this.personalInfoService.updateEvaluationGradeByUserId(object);
    }

    @ApiOperation("后台获取测评师资料---分页")
    @PostMapping("/background/getList")
    public TableDataInfo backgroundList(@RequestBody PersonalInfoSearchVo searchVo) {
        startPage();
        List<PersonalInfo> list = personalInfoService.getListBySearchVo(searchVo);
        return getDataTable(list);
    }

    @ApiOperation("后台获取测评师资料---分页")
    @PostMapping("/page/list")
    public TableDataInfo getList(@RequestBody PersonalInfoSearchVo searchVo) {
        startPage();
        List<PersonalInfo> list = personalInfoService.getList(searchVo);
        return getDataTable(list);
    }

    @ApiOperation("后台修改测评师等级")
    @PostMapping("/edit/evaluationGrade")
    public AjaxResult editEvaluationGrade(@RequestBody PersonalInfoSearchVo searchVo) {
        if (StringUtils.isBlank(searchVo.getEvaluationGrade())) {
            return AjaxResult.error("评估等级不能为空");
        }
        return personalInfoService.editEvaluationGrade(searchVo.getIds(), searchVo.getEvaluationGrade());
    }

    @ApiOperation("后台获取测评师资料")
    @GetMapping("/info/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            return AjaxResult.error("id不能为空");
        }
        JSONObject object = new JSONObject();
        object.put("id", id);
        return this.personalInfoService.getPersonalInfo(object);
    }

}
